﻿<UserControl x:Class="CredentialsManager.Views.UserListView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:ff="clr-namespace:FunctionalFun.UI.Behaviours"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <DockPanel LastChildFill="True" Style="{StaticResource DisableIfLoadingStyle}">
        <StackPanel DockPanel.Dock="Top" Margin="0, 0, 0, 10">
            <Label FontWeight="Bold">Users</Label>
            <Label>Filter:</Label>
            <TextBox Text="{Binding Filter, UpdateSourceTrigger=PropertyChanged}" />
        </StackPanel>
        <WrapPanel DockPanel.Dock="Bottom" Orientation="Horizontal" HorizontalAlignment="Center">
            <Button Content="Ne_w user" Command="{Binding CreateUserCommand}" 
                    Padding="5" Margin="5" ToolTip="New user (Alt+W)" />
            <Button Content="Update" Command="{Binding UpdateUserCommand}" Padding="5" Margin="5">
                <Button.Style>
                    <Style TargetType="{x:Type Button}">
                        <Setter Property="IsEnabled" Value="False" />
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding SelectedUsers.Count}" Value="1">
                                <Setter Property="IsEnabled" Value="True" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Button.Style>
            </Button>
            <Button Content="Update password question" 
                    Command="{Binding UpdatePasswordQuestionCommand}" Padding="5" Margin="5">
                <Button.Style>
                    <Style TargetType="{x:Type Button}">
                        <Setter Property="IsEnabled" Value="False" />
                        <Setter Property="Visibility" Value="Collapsed" />
                        <Style.Triggers>
                            <MultiDataTrigger>
                                <MultiDataTrigger.Conditions>
                                    <Condition Binding="{Binding RequiresQuestionAndAnswer}" Value="True" />
                                    <Condition Binding="{Binding EnablePasswordRetrieval}" Value="True" />
                                </MultiDataTrigger.Conditions>
                                <MultiDataTrigger.Setters>
                                    <Setter Property="Visibility" Value="Visible" />
                                </MultiDataTrigger.Setters>
                            </MultiDataTrigger>
                            <DataTrigger Binding="{Binding SelectedUsers.Count}" Value="1">
                                <Setter Property="IsEnabled" Value="True" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Button.Style>
            </Button>
            <Button Content="Delete" Command="{Binding Path=DataContext.DeleteUserCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" 
                    DataContext="{Binding SelectedUsers}" Padding="5" Margin="5" Style="{StaticResource EnableIfItemsSelected}" />
            <Button Content="Reset pass_word" Command="{Binding ResetPasswordCommand}" Padding="5" Margin="5">
                <Button.Style>
                    <Style TargetType="{x:Type Button}">
                        <Setter Property="IsEnabled" Value="False" />
                        <Setter Property="Visibility" Value="Collapsed" />
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding EnabledPasswordReset}" Value="True">
                                <Setter Property="Visibility" Value="Visible" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding SelectedUsers.Count}" Value="1">
                                <Setter Property="IsEnabled" Value="True" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Button.Style>
            </Button>
            <Button Content="Change password" Command="{Binding ChangePasswordCommand}" Padding="5" Margin="5">
                <Button.Style>
                    <Style TargetType="{x:Type Button}">
                        <Setter Property="IsEnabled" Value="False" />
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding SelectedUsers.Count}" Value="1">
                                <Setter Property="IsEnabled" Value="True" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Button.Style>                
            </Button>
        </WrapPanel>
        <ListBox 
            ItemsSource="{Binding Users}" 
            SelectionMode="Extended"
            ff:MultiSelectorBehaviours.SynchronizedSelectedItems="{Binding SelectedUsers}"
            ItemTemplate="{StaticResource UserDataTemplate}" />
    </DockPanel>
</UserControl>
